//
//  WmcActionType.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/20.
//

import RxSwift
import Toast_Swift
import UIKit
import YPBedrock

// MARK: - [WmcActionType]

/// 全局通用事件类型
/// 用于外部设置事件
public enum WmcActionType {
    /// 使用水印相机内部默认处理
    case `default`

    /// 自定义跳转页面
    /// - Parameters:
    ///   - view: 自定义视图
    case pushCustom(view: WmcCustomViewProtocol)

    /// 直接返回
    /// - Parameters:
    ///   - animated: 是否需要返回动画
    case pop(animated: Bool)

    /// 自定义拉起弹窗视图
    case presentSheet(view: UIView,
                      layoutBlock: YPBlock.OneParam<ConstraintMaker>,
                      isTouchMaskDismiss: Bool = true,
                      maskBackgroundColor: UIColor = UIColor.black.yp.alpha(0.3),
                      hollowRects: [WmcPresentVC.Config.HollowRect] = [],
                      touchMaskBlock: YPBlock.Empty = {})

    /// 自定义弹出弹窗视图
    case presentAlert(view: UIView,
                      layoutBlock: YPBlock.OneParam<ConstraintMaker>,
                      isTouchMaskDismiss: Bool = true,
                      maskBackgroundColor: UIColor = UIColor.black.yp.alpha(0.3),
                      hollowRects: [WmcPresentVC.Config.HollowRect] = [],
                      touchMaskBlock: YPBlock.Empty = {})

    /// 弹出一个Toast进行提示
    /// - Parameters:
    ///   - text: 文字
    ///   - duration: 持续时间
    ///   - position: 方位 上中下
    case toast(text: String,
               duration: TimeInterval = 1.3,
               position: ToastPosition = .center)

    /// 拦截/放弃，即是不处理该事件
    /// 或者在返回此值之前可以做自定义处理，即重写事务
    case abandon
}

// MARK: - =========================⚠️以下对接业务方无需查看或了解⚠️=========================

extension WmcActionType {
    /// 进行默认事务处理
    func transactionDefaultCase(_ defaultBlock: YPBlock.Empty) {
        switch self {
        case .default:
            defaultBlock()

        case let .pushCustom(view):
            WmcNavigatorManager.pushCustom(view)

        case let .presentSheet(view: view,
                               layoutBlock: layoutBlock,
                               isTouchMaskDismiss: isTouchMaskDismiss,
                               maskBackgroundColor: maskBackgroundColor,
                               hollowRects: hollowRects,
                               touchMaskBlock: touchMaskBlock):
            let config = createPresentConfig(style: .sheet(),
                                             view: view,
                                             layoutBlock: layoutBlock,
                                             isTouchMaskDismiss: isTouchMaskDismiss,
                                             backgroundColor: maskBackgroundColor,
                                             hollowRects: hollowRects,
                                             touchMaskBlock: touchMaskBlock)
            let presentVC = WmcPresentVC(config: config)
            presentVC.show()

        case let .presentAlert(view: view,
                               layoutBlock: layoutBlock,
                               isTouchMaskDismiss: isTouchMaskDismiss,
                               maskBackgroundColor: maskBackgroundColor,
                               hollowRects: hollowRects,
                               touchMaskBlock: touchMaskBlock):
            var config = createPresentConfig(style: .alert(),
                                             view: view,
                                             layoutBlock: layoutBlock,
                                             isTouchMaskDismiss: isTouchMaskDismiss,
                                             backgroundColor: maskBackgroundColor,
                                             hollowRects: hollowRects,
                                             touchMaskBlock: touchMaskBlock)
            config.isTouchMaskDismiss = false
            let presentVC = WmcPresentVC(config: config)
            presentVC.show()

        case let .pop(animated):
            let currentVC = YPApp.CurrentVC
            if let wmcBaseVC = currentVC as? WmcBaseVC {
                wmcBaseVC.popVC(animated: animated)
            } else {
                currentVC?.navigationController?.popViewController(animated: animated)
            }

        case let .toast(text, duration, position):
            WmcHudManager.toast(text, duration: duration, position: position)

        case .abandon:
            break
        }
    }

    /// 创建模态配置器
    fileprivate func createPresentConfig(style: WmcPresentVC.Style,
                                         view: UIView,
                                         layoutBlock: @escaping YPBlock.OneParam<ConstraintMaker>,
                                         isTouchMaskDismiss: Bool = true,
                                         backgroundColor: UIColor = UIColor.black.yp.alpha(0.3),
                                         hollowRects: [WmcPresentVC.Config.HollowRect],
                                         touchMaskBlock: @escaping YPBlock.Empty = {}) -> WmcPresentVC.Config
    {
        let config: WmcPresentVC.Config = .init(style: style,
                                                contentView: view,
                                                contentLayoutBlock: layoutBlock,
                                                isTouchMaskDismiss: isTouchMaskDismiss,
                                                touchMaskBlock: touchMaskBlock,
                                                backgroundColor: backgroundColor,
                                                hollowRects: hollowRects)
        return config
    }
}
